package queue;

import java.util.LinkedList;
import java.util.Scanner;

//环形链表队列
public class CircleLinkedQueueDemo {
    public static void main(String[] args) {
        //创建队列
        LinkedQueue arrayQueue = new LinkedQueue(4);//设置4 有效数据个数为3
        char key = ' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列获取数据");
            System.out.println("h(head):查看队列头部数据");
            key = scanner.next().charAt(0);//接受的字符
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入1个数");
                    int n = scanner.nextInt();
                    arrayQueue.addQueue(n);
                    break;
                case 'g':
                    try {
                        int queue = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", queue);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'h':
                    try {
                        int queue = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", queue);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;

                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }

    static class LinkedQueue {
        private int maxSize;//数组的最大容量
        private int front;//指向队列第一个元素
        private int rear;//指向队列最后一个元素的下一个位置
        private LinkedList<Integer> linkedList;//模拟的队列

        //队列构造器
        public LinkedQueue(int arrmaxSize) {
            maxSize = arrmaxSize;
            linkedList = new LinkedList<>();
            front = 0;
            rear = 0;
        }

        //判断是否满
        public boolean isFull() {
            return (rear + 1) % maxSize == front;
        }

        //判断是否为空
        public boolean isEmpty() {
            return rear == front;
        }

        //添加数据
        public void addQueue(int n) {
            if (isFull()) {
                System.out.println("队列已满");
                return;
            }
            linkedList.add(n);
            rear = (rear + 1) % maxSize;
        }

        //获取数据
        public int getQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空，不能取数据。");
            }
            int value = linkedList.get(front);
            front = (front + 1) % maxSize;
            return value;
        }

        //显示队列的所有数据
        public void showQueue() {
            //遍历
            if (isEmpty()) {
                System.out.println("队列为空");
                return;
            }
            //从front开始遍历
            for (int i = front; i < front + size(); i++) {
                System.out.printf("linkedList[%d] = %d\n", i % maxSize, linkedList.get(i % maxSize));

            }
        }

        //获取有效数据个数
        public int size() {
            return (rear + maxSize - front) % maxSize;
        }

        //显示队列的头数据
        public int headQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空，不能取数据。");
            }
            return linkedList.get(front + 1);
        }
    }
}
